package com.hulizuofang.wol.chat.client.ui.chat;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;
import com.hulizuofang.wol.chat.client.bundle.ClientResourceBundle;
import com.hulizuofang.wol.chat.client.gin.ClientI18nMessages;
import com.hulizuofang.wol.chat.client.ui.event.setlogin.HasSetLoginHandlers;
import com.hulizuofang.wol.chat.client.ui.event.setlogin.SetLoginEvent;
import com.hulizuofang.wol.chat.client.ui.event.setlogin.SetLoginHandler;

public class SetLoginPanel extends Composite implements HasSetLoginHandlers {
	private final FlowPanel container;
	private TextBox textbox;
	private String textBoxDefaultValue;
	private Button button;

	@Inject
	public SetLoginPanel(ClientI18nMessages i18n, final ClientResourceBundle bundle) {
		container = new FlowPanel();
		
		textbox = new TextBox();
		textBoxDefaultValue = i18n.setLogin();
		textbox.setValue(textBoxDefaultValue);
		
		textbox.addStyleName(bundle.css().untouchedTextbox());

		textbox.addFocusHandler(new FocusHandler() {
			@Override
			public void onFocus(FocusEvent event) {
				if (isNotSet()) {
					textbox.setValue("");
					textbox.removeStyleName(bundle.css().untouchedTextbox());
				}
			}
		});
		textbox.addBlurHandler(new BlurHandler() {
			@Override
			public void onBlur(BlurEvent event) {
				if (isEmpty()) {
					textbox.setValue(textBoxDefaultValue);
					textbox.addStyleName(bundle.css().untouchedTextbox());
				}
			}
		});
		
		container.add(textbox);
		
		button = new Button(i18n.connect());
		
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isReady()) {
					SetLoginEvent.fire(SetLoginPanel.this, textbox.getValue());
				}
			}
		});
		textbox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER && isReady()) {
					SetLoginEvent.fire(SetLoginPanel.this, textbox.getValue());
				}
			}
		});
		
		container.add(button);
		
		initWidget(container);
	}
	
	public boolean isNotSet() {
		return textBoxDefaultValue.equals(textbox.getValue());
	}
	
	public boolean isEmpty() {
		return "".equals(textbox.getValue());
	}
	
	public boolean isReady() {
		return ! isEmpty() && ! isNotSet();
	}

	@Override
	public HandlerRegistration addSetLoginHandler(SetLoginHandler handler) {
		return addHandler(handler, SetLoginEvent.getType());
	}
}
